package com.xyht.medicine_management_system.common.commonReturn;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xyht.medicine_management_system.common.desEncodeAndDecode.DesCbcUtil;
import com.xyht.medicine_management_system.common.desEncodeAndDecode.SecretConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.xyht.medicine_management_system.common.desEncodeAndDecode.constant.ConfigConstant.DES_SECRET_KEY;


/**
 * 通用返回对象
 * Created by macro on 2019/4/19.
 */
@Component
public class CommonResult<T> implements ResponseResult {
    static SecretConfig secretConfig;

    @Autowired
    public void setJwtTokenUtil(SecretConfig secretConfig) {
        CommonResult.secretConfig = secretConfig;
    }
    private boolean success;
    private long code;
    private String msg;
    private T data;

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public boolean getSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    private int total = 0;

    protected CommonResult() {
    }

    public CommonResult(boolean success, long code, String msg, T data, int total) {
        DesCbcUtil desCbcUtil = new DesCbcUtil();
        this.success = success;
        this.code = code;
        this.msg = msg;
        if (secretConfig.isEncode_scan_switch()){
            this.data = desCbcUtil.encryptDES(JSONObject.toJSONStringWithDateFormat(data,"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue),DES_SECRET_KEY);
        }else {
            this.data = data;
        }
        this.total = total;
    }

    public CommonResult(boolean success, long code, String msg, T data) {
        DesCbcUtil desCbcUtil = new DesCbcUtil();
        this.success = success;
        this.code = code;
        this.msg = msg;
        if (secretConfig.isEncode_scan_switch()){
            this.data = desCbcUtil.encryptDES(JSONObject.toJSONStringWithDateFormat(data,"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue),DES_SECRET_KEY);
        }else {
            this.data = data;
        }
    }

    public CommonResult(boolean success, long code, String msg) {
        this.success = success;
        this.code = code;
        this.msg = msg;
    }

    /**
     * 成功返回结果
     *
     */
    public static <T> CommonResult<T> success() {
        return new CommonResult<T>(CommonCodeEnum.SUCCESS.getSuccess(),
                CommonCodeEnum.SUCCESS.getCode(),
                CommonCodeEnum.SUCCESS.getMsg());
    }

    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<T>(CommonCodeEnum.SUCCESS.getSuccess(),
                CommonCodeEnum.SUCCESS.getCode(),
                CommonCodeEnum.SUCCESS.getMsg(),
                data);
    }

    public static <T> CommonResult<T> success(T data, int total) {
        return new CommonResult<T>(CommonCodeEnum.SUCCESS.getSuccess(),
                CommonCodeEnum.SUCCESS.getCode(),
                CommonCodeEnum.SUCCESS.getMsg(),
                data,
                total);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     */
    public static <T> CommonResult<T> failed(CommonCodeEnum errorCode) {
        return new CommonResult<T>(errorCode.getSuccess(),
                errorCode.getCode(),
                errorCode.getMsg());
    }
    public static <T> CommonResult<T> failed(String msg) {
        return new CommonResult<T>(false,
                4000,
                msg);
    }


    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
